//
//  HomeChooseHeaderView.m
//  GuShiNews
//
//  Created by huachun on 2019/8/22.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "HomeChooseHeaderView.h"

@interface HomeChooseHeaderView (){
    NSInteger index;
}

@property (nonatomic, strong) NSMutableArray *button_list;

@end

@implementation HomeChooseHeaderView

/* 在构造方法中，创建UI*/
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        CALayer *bg_view = [CALayer new];
        bg_view.backgroundColor = [UIColor whiteColor].CGColor;
        bg_view.frame=CGRectMake(0, 0, RECRWITCH, 43);
        [self.layer addSublayer:bg_view];
        
        NSArray *linlist = @[@"关注",@"推荐",@"热门"];
        
        index = 1;
        
        CGFloat w = RECRWITCH/linlist.count;
        
        self.button_list = [NSMutableArray new];
        
        [linlist enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(w*idx, 3, w, 40)];
            button.tag = idx;
            [button setTitle:obj forState:UIControlStateNormal];
            [button addTarget:self action:@selector(chooseindex:) forControlEvents:UIControlEventTouchUpInside];
            [button.titleLabel setFont:[UIFont boldSystemFontOfSize:16]];
            [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [self.button_list addObject:button];
            [self addSubview:button];
            if (idx==self->index) {
                [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }
        }];

    }
    return self;
}

- (void)chooseindex:(UIButton *)sender{
    if (sender.tag == index) {
        return;
    }
    
    index=sender.tag;
    [self.button_list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *button = obj;
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        if (idx==self->index) {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }];
    
    CGFloat w = RECRWITCH/self.button_list.count;
    
    _selectblock (index);
}

@end
